//package com.example.es;
//
//import co.elastic.clients.elasticsearch.core.BulkRequest;
//import co.elastic.clients.elasticsearch.core.DeleteRequest;
//import co.elastic.clients.elasticsearch.core.GetRequest;
//import co.elastic.clients.elasticsearch.core.IndexRequest;
//import co.elastic.clients.elasticsearch.indices.DeleteIndexResponse;
//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.JSONObject;
//import org.elasticsearch.action.ActionListener;
//import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
//import org.elasticsearch.action.bulk.BulkResponse;
//import org.elasticsearch.action.search.SearchRequest;
//import org.elasticsearch.action.search.SearchResponse;
//import org.elasticsearch.action.update.UpdateRequest;
//import org.elasticsearch.client.RequestOptions;
//import org.elasticsearch.client.RestHighLevelClient;
//import org.elasticsearch.client.indices.CreateIndexRequest;
//import org.elasticsearch.client.indices.CreateIndexResponse;
//import org.elasticsearch.common.settings.Settings;
//import org.elasticsearch.common.xcontent.XContentBuilder;
//import org.elasticsearch.common.xcontent.XContentFactory;
//import org.elasticsearch.common.xcontent.XContentType;
//import org.elasticsearch.index.query.BoolQueryBuilder;
//import org.elasticsearch.index.query.QueryBuilders;
//import org.elasticsearch.index.query.RangeQueryBuilder;
//import org.elasticsearch.search.SearchHit;
//import org.elasticsearch.search.SearchHits;
//import org.elasticsearch.search.builder.SearchSourceBuilder;
//import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
//import org.junit.jupiter.api.Test;
//import org.junit.runner.RunWith;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.test.context.SpringBootTest;
//import org.springframework.test.context.junit4.SpringRunner;
//
//import java.io.IOException;
//import java.util.List;
//import java.util.Map;
//
///**
// * @author chenming
// * Date  2023/1/12 9:45
// * @version 1.0
// * description
// */
//@RunWith(SpringRunner.class)
//@SpringBootTest(classes = { ddd.class })
//public class ddd {
//        @Autowired
//        private RestHighLevelClient client;
//
//
//        public static String INDEX_TEST = null;
//        public static String TYPE_TEST = null;
//        public static ddd tests = null;
//        public static List<ddd> testsList = null;
//
//        @Test
//        public void contextLoads() throws IOException, CloneNotSupportedException {
////        // 判断是否存在索引
////        existsIndex("indexName");
////        // 创建索引
////        createIndex("house", "house-info");
////        // 删除索引
////        deleteIndex("house");
////        // 判断数据是否存在
////        exists("user", "user-info", "JO3hP24BlvWqEof7y5BF");
////        // 根据ID获取数据
////        get("index01", "type", "201");
////
////        List<String> idList = null;
////        List<Map<String, Object>> dataList = null;
////        // //批量更新
////        bulkUpdate("index_name", "index_type", idList, dataList);
////        // 批量添加
////        bulkAdd("index_name", "index_type", idList, dataList);
////        // 批量删除
////       List<Map<String, Object>> dataList = null;
////       List<String> idList = new ArrayList<String>();
////       idList.add("ZsSfRW4B3jWdK-k5x4lo");
////       idList.add("ZcSZRW4B3jWdK-k5E4ld");
////        bulkDelete("user", "user-info", idList, dataList);
////
////        //坐标范围查询
////        searchPoint();
////        //关键字查询
////        search("蒋", "user-info", "user");
//
//            client.close();
//        }
//
//        /**
//         * 创建索引
//         *
//         * @param index_name
//         * @param index_type
//         * @throws IOException
//         */
//        private void createIndex(String index_name, String index_type) throws IOException {
//            CreateIndexRequest request = new CreateIndexRequest(index_name);// 创建索引
//            // 创建的每个索引都可以有与之关联的特定设置===================设置主片与副本数======================
//            //主片的个数(默认5个)确定是不会变的，副本数(默认1个)是可以改变的
//            request.settings(Settings.builder().put("index.number_of_shards", 3).put("index.number_of_replicas", 1));
//            XContentBuilder xContentBuilder = XContentFactory.jsonBuilder();
//            xContentBuilder.startObject()
//                    // 索引库名（类似数据库中的表），可以指定字段的类型，String中text是分词的，keyword不分词
//                    .startObject(index_type).startObject("properties")
//                    .startObject("doc.haCode").field("type", "keyword").endObject()
//                    .startObject("doc.haStatus").field("type", "integer").field("index", false).endObject()
//                    .startObject("doc.haName").field("type", "text").endObject()
//                    .startObject("doc.haAddr").field("type", "text").endObject()
//                    .startObject("doc.haPrice").field("type", "float").field("index", false).endObject()
//                    .startObject("doc.location").field("type", "geo_point").endObject()
//                    //不参与索引创建
//                    .startObject("doc.haImage").field("type", "text").field("index", false).endObject()
//                    .startObject("doc.haDate").field("type", "date").endObject()
//                    .endObject().endObject().endObject();
//            // 创建索引时创建文档类型映射==========================定义mapping,指定字段类型，如一些特殊字段=========================
//            request.mapping(index_type, xContentBuilder);
//
//            // 异步执行
//            // 异步执行创建索引请求需要将CreateIndexRequest实例和ActionListener实例传递给异步方法：
//            // CreateIndexResponse的典型监听器如下所示：
//            // 异步方法不会阻塞并立即返回。
//            ActionListener<CreateIndexResponse> listener = new ActionListener<CreateIndexResponse>() {
//                @Override
//                public void onResponse(CreateIndexResponse createIndexResponse) {
//                    // 如果执行成功，则调用onResponse方法;
//                }
//
//                @Override
//                public void onFailure(Exception e) {
//                    // 如果失败，则调用onFailure方法。
//                }
//            };
//            CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
//            System.out.println("createIndex: " + JSON.toJSONString(createIndexResponse));
//            xContentBuilder.close();
//            // client.indices().createAsync(request, RequestOptions.DEFAULT,
//            // listener);//要执行的CreateIndexRequest和执行完成时要使用的ActionListener
//        }
//
//        /**
//         * 判断索引是否存在
//         *
//         * @param index_name
//         * @return
//         * @throws IOException
//         */
//        public boolean existsIndex(String index_name) throws IOException {
//            GetIndexRequest request = new GetIndexRequest();
//            request.indices(index_name);
//            boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
//            System.out.println("existsIndex: " + exists);
//            return exists;
//        }
//
//        /**
//         * 删除索引
//         *
//         * @param index
//         * @return
//         * @throws IOException
//         */
//        public void deleteIndex(String index_name) throws IOException {
//            DeleteIndexRequest request = new DeleteIndexRequest(index_name);
//            DeleteIndexResponse deleteIndexResponse = client.indices().delete(request, RequestOptions.DEFAULT);
//            System.out.println("deleteIndex: " + JSON.toJSONString(deleteIndexResponse));
//        }
//
//        /**
//         * 判断记录是否存在
//         *
//         * @param index
//         * @param type
//         * @param tests
//         * @return
//         * @throws IOException
//         */
//        public boolean exists(String index_name, String index_type, String index_id) throws IOException {
//            GetRequest getRequest = new GetRequest(index_name, index_type, index_id);
//            getRequest.fetchSourceContext(new FetchSourceContext(false));
//            getRequest.storedFields("_none_");
//            boolean exists = client.exists(getRequest, RequestOptions.DEFAULT);
//            System.out.println("exists: " + exists);
//            return exists;
//        }
//
//        /**
//         * 根据ID获取记录信息
//         *
//         * @param index
//         * @param type
//         * @param id
//         * @throws IOException
//         */
//        public void get(String index_name, String index_type, String index_id) throws IOException {
//            GetRequest getRequest = new GetRequest(index_name, index_type, index_id);
//            GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
//            System.out.println("get: " + JSON.toJSONString(getResponse));
//        }
//
//        /**
//         * 批量增加 addTestList方法封装list数据
//         *
//         * @throws IOException
//         */
//        private void bulkAdd(String index_name, String index_type, List<String> idList, List<Map<String, Object>> dataList)
//                throws IOException {
//            BulkRequest bulkAddRequest = new BulkRequest();
//            for (int i = 0; i < dataList.size(); i++) {
//                IndexRequest indexRequest = new IndexRequest(index_name, index_type, idList.get(i));
//                indexRequest.source(JSON.toJSONString(dataList.get(i)), XContentType.JSON);
//                bulkAddRequest.add(indexRequest);
//            }
//            BulkResponse bulkAddResponse = client.bulk(bulkAddRequest, RequestOptions.DEFAULT);
//            System.out.println("bulkAddResponse: " + JSON.toJSONString(bulkAddResponse));
//        }
//
//        /**
//         * 批量更新
//         *
//         * @param index
//         * @param type
//         * @param tests
//         * @throws IOException
//         */
//        public void bulkUpdate(String index_name, String index_type, List<String> idList,
//                               List<Map<String, Object>> dataList) throws IOException {
//            BulkRequest bulkUpdateRequest = new BulkRequest();
//            for (int i = 0; i < dataList.size(); i++) {
//                UpdateRequest updateRequest = new UpdateRequest(index_name, index_type, idList.get(i));
//                updateRequest.doc(JSON.toJSONString(dataList.get(i)), XContentType.JSON);
//                bulkUpdateRequest.add(updateRequest);
//            }
//            BulkResponse bulkUpdateResponse = client.bulk(bulkUpdateRequest, RequestOptions.DEFAULT);
//            System.out.println("bulkUpdate: " + JSON.toJSONString(bulkUpdateResponse));
//        }
//
//        /**
//         * 删除记录
//         *
//         * @param index
//         * @param type
//         * @param id
//         * @throws IOException
//         */
//        public void bulkDelete(String index_name, String index_type, List<String> idList,
//                               List<Map<String, Object>> dataList) throws IOException {
//            BulkRequest bulkDeleteRequest = new BulkRequest();
//            for (int i = 0; i < idList.size(); i++) {
//                DeleteRequest deleteRequest = new DeleteRequest(index_name, index_type, idList.get(i));
//                bulkDeleteRequest.add(deleteRequest);
//            }
//            BulkResponse bulkDeleteResponse = client.bulk(bulkDeleteRequest, RequestOptions.DEFAULT);
//            System.out.println("bulkDelete: " + JSON.toJSONString(bulkDeleteResponse));
//        }
//
//        /**
//         * 搜索
//         *
//         * @param index 要搜索的索引库
//         * @param type 要搜索的索引库类型
//         * @param name  要搜索的关键字
//         * @throws IOException
//         */
//        public void search(String name, String type, String index) throws IOException {
//            //query查询：
//            //match查询：知道分词器存在，会对查询的关键字分词；
//            //team一个关键词/teams多个关键词查询：不知道分词器，不会对查询的关键字分词; 较精确的查询
//            //例子：条件是‘我你’ team查询的是含有‘我你’的，match查询含有‘我你’‘我’‘你’的都能查询出
//            //QueryBuilders.matchQuery("name", name)
//
//            //filter查询：不计算相关性，且有cache，速度比query查询快
//            // boolBuilder多条件查询：must相当于and, should相当于or，mustnot不符合条件的
//
//            //聚合查询
//            // sum min max avg cardinality基数(类似去重之后的数量) teams分组
//
//            // matchQuery(提高召回率，关键字会被分词),
//            // matchPhraseQuery(关键字不会分词), match_phrase提高精准度
//            // matchQuery单一查询QueryBuilders.matchQuery("name", name)，
//            // multiMatchQuery匹配多列查询QueryBuilders.multiMatchQuery("music","name","interest")，
//            // wildcardQuery模糊匹配查询QueryBuilders.wildcardQuery("name", "*jack*") *多个 ?一个
//            // QueryBuilders.matchPhraseQuery("字段名字", "前缀");//前缀查询
//            // QueryBuilders.fuzzyQuery("字段名字", "关键字");    //模糊查询，跟关键字类似的都可以查询出来 --> 关键字：tet 有可能会搜寻出 test text等
//            // rangeQuery区间查询
//            // geoDistanceQuery经纬度范围商家
//            // 排序：ScoreSortBuilder，FieldSortBuilder
//            //关键字查询
//            BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery();
//            // boolBuilder.must(QueryBuilders.matchQuery("name", name)); //
//            // 这里可以根据字段进行搜索，must表示符合条件的，相反的mustnot表示不符合条件的
//            // boolBuilder.must(QueryBuilders.matchQuery("sex", name));
//            // boolBuilder.must(QueryBuilders.matchQuery("id", tests.getId().toString()));
//
//            // boolQueryBuilder.must(QueryBuilders.termQuery("field","value"));
//            // boolQueryBuilder.must(QueryBuilders.wildcardQuery("field","value"));
//            // boolQueryBuilder.must(QueryBuilders.rangeQuery("field").gt("value"));
//            // boolQueryBuilder.must(QueryBuilders.termsQuery("field","value"));
//
////        boolBuilder.should(boolBuilder.filter(QueryBuilders.matchPhraseQuery("name", name)));
////        boolBuilder.should(boolBuilder.filter(QueryBuilders.matchPhraseQuery("sex", name)));
////        boolBuilder.should(QueryBuilders.matchPhraseQuery("sex", name));
//            boolBuilder.should(QueryBuilders.matchPhraseQuery("doc.username", name));
//            // RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("id");
//            // //区间查询，都是闭区间
//            // rangeQueryBuilder.gte(1);
//            // rangeQueryBuilder.lte(1);
//            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//            sourceBuilder.query(boolBuilder);
//            // sourceBuilder.query(rangeQueryBuilder);
//            sourceBuilder.from(0);
//            sourceBuilder.size(100); // 获取记录数，默认10
//            // sourceBuilder.sort("id", SortOrder.ASC);
////        sourceBuilder.fetchSource(new String[] { "id", "name", "sex", "age" }, new String[] {}); // 第一个是获取字段，第二个是过滤的字段，默认获取全部
//            sourceBuilder.fetchSource(new String[] { "doc.username", "doc.password"}, new String[] {}); // 第一个是获取字段，第二个是过滤的字段，默认获取全部
//            SearchRequest searchRequest = new SearchRequest(index);
//            searchRequest.types(type);
//            searchRequest.source(sourceBuilder);
//            SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
//            System.out.println("search: " + JSON.toJSONString(response));
//            SearchHits hits = response.getHits();
//            SearchHit[] searchHits = hits.getHits();
//            for (SearchHit hit : searchHits) {
//                System.out.println("search -> " + hit.getSourceAsString());
//                System.out.println("search -> " + hit.getSourceAsMap().get("doc"));
//                UserEntity parseObject = JSONObject.parseObject(JSONObject.toJSONString(hit.getSourceAsMap().get("doc")), UserEntity.class);
//                System.out.println("search -> " + parseObject.getUsername());
//            }
//        }
//
//        /**
//         * 范围查询，左右都是闭集
//         *
//         * @param fieldKey
//         * @param start
//         * @param end
//         * @return
//         */
//        public RangeQueryBuilder rangeMathQuery(String fieldKey, String start, String end) {
//            RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery(fieldKey);
//            rangeQueryBuilder.gte(start);
//            rangeQueryBuilder.lte(end);
//            return rangeQueryBuilder;
//        }
//
//        //坐标查询 范围查询
//        public void searchPoint() throws IOException {
//            double a = 40.215193;
//            double b = 116.680852;
//            GeoDistanceQueryBuilder builder = QueryBuilders.geoDistanceQuery("location");
//            builder.point(a, b);
//            builder.distance(1000, DistanceUnit.MILES);
//            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//            sourceBuilder.query(builder);
//            GeoDistanceSortBuilder sort = SortBuilders.geoDistanceSort("location", a, b).order(SortOrder.ASC)
//                    .unit(DistanceUnit.KILOMETERS);
//            sourceBuilder.sort("id", SortOrder.ASC);
//            SearchRequest searchRequest = new SearchRequest("index1");
//            searchRequest.types("type1");
//            searchRequest.source(sourceBuilder);
//            SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
//            SearchHits hits = response.getHits();
//            SearchHit[] searchHits = hits.getHits();
//            for (SearchHit hit : searchHits) {
//                System.out.println("search -> " + hit.getSourceAsString());
//            }
//        }
//
//        /**
//         * 创建新增数据
//         *
//         * @throws IOException
//         */
//        public void addTestList() throws IOException {
//            double lat = 38.929986;
//            double lon = 113.395645;
//            for (long i = 201; i < 202; i++) {
//                double max = 0.00001;
//                double min = 0.000001;
//                Random random = new Random();
//                double s = random.nextDouble() % (max - min + 1) + max;
//                DecimalFormat df = new DecimalFormat("######0.000000");
//                // System.out.println(s);
//                String lons = df.format(s + lon);
//                String lats = df.format(s + lat);
//                Double dlon = Double.valueOf(lons);
//                Double dlat = Double.valueOf(lats);
//                Tests tests = new Tests();
//                tests.setId(i);
//                tests.setName("名字啊" + i);
//                tests.setSex("电话啊" + i);
//                GeoPoint location = new GeoPoint();
//                location.setLat(dlat);
//                location.setLon(dlon);
//                tests.setLocation(location);
//                testsList.add(tests);
//            }
//        }
//
//    private class GetResponse {
//    }
//}
